package com.ecnu.web.controller.project;

import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.system.domain.ProjectPlan;
import com.ecnu.system.domain.dto.ProjectPlanDTO;
import com.ecnu.system.domain.dto.ProjectPlanStatusDTO;
import com.ecnu.system.domain.vo.ProjectPlanVO;
import com.ecnu.system.service.IProjectPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "项目计划管理")
@RestController
@RequestMapping("/project/plan")
public class ProjectPlanController extends BaseController {
    @Autowired
    private IProjectPlanService projectPlanService;

    @ApiOperation(value = "查询项目计划列表")
    @PostMapping("/list")
    public TableDataInfo getList(@Valid @RequestBody ProjectPlanDTO dto) {
        LoginUser loginUser = getLoginUser();
        return projectPlanService.getList(dto,  loginUser);
    }

    @ApiOperation(value = "编辑计划")
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('project:plan:edit')")
    public AjaxResult edit(@Valid @RequestBody ProjectPlanVO dto) {
        return projectPlanService.edit(dto);
    }

    @ApiOperation(value = "删除计划")
    @PostMapping("/del")
    @PreAuthorize("@ss.hasPermi('project:plan:del')")
    public AjaxResult del(@RequestBody String[] ids) {
        List<ProjectPlan> projects = Arrays.stream(ids)
                .map(id -> {
                    ProjectPlan plan = new ProjectPlan();
                    plan.setId(id);
                    plan.setDelFlag("2");
                    return plan;
                })
                .collect(Collectors.toList());
        return projectPlanService.updateBatchById(projects) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
    @ApiOperation(value = "计划明细")
    @GetMapping("/info")
    public AjaxResult info(@RequestParam("id") String id) {
        return projectPlanService.info(id);
    }

    @ApiOperation(value = "计划进度调整")
    @PostMapping("/saveStatus")
    public AjaxResult saveStatus(@RequestBody ProjectPlanStatusDTO dto) {
        ProjectPlan projectPlan = projectPlanService.getById(dto.getId());
        if (projectPlan == null) {
            return AjaxResult.error("计划不存在");
        }
        ProjectPlan plan = new ProjectPlan();
        plan.setId(dto.getId());
        plan.setStatus(dto.getStatus());
        plan.setScoring(dto.getScoring());
        plan.setProgress(dto.getProgress());
        if (dto.getProgress()!=null) {
            plan.setIsManual("1");
        }
        return projectPlanService.updateById(plan) ? AjaxResult.success("保存成功") : AjaxResult.error("保存失败");
    }
}
